home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / HDRLighting / GlareDefD3D.cpp next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  11.6 KB  |  430 lines

  1. // File provided by Masaki Kawase 
  2. // http://www.daionet.gr.jp/~masa/rthdribl/
  3.  
  4. // GlareDefD3D.cpp : Glare information definition
  5. //
  6. #include "dxstdafx.h"
  7. #include "GlareDefD3D.h"
  8.  
  9. #define _Rad    D3DXToRadian
  10.  
  11. // Static star library information
  12. static STARDEF s_aLibStarDef[NUM_BASESTARLIBTYPES] =
  13. {
  14.     //    star name                lines    passes    length    attn    rotate            bRotate
  15.     {    TEXT("Disable"),        0,        0,        0.0f,    0.0f,    _Rad(00.0f),    false,    },    // STLT_DISABLE
  16.  
  17.     {    TEXT("Cross"),            4,        3,        1.0f,    0.85f,    _Rad(0.0f),        true,    },    // STLT_CROSS
  18.     {    TEXT("CrossFilter"),    4,        3,        1.0f,    0.95f,    _Rad(0.0f),        true,    },    // STLT_CROSS
  19.     {    TEXT("snowCross"),        6,        3,        1.0f,    0.96f,    _Rad(20.0f),    true,    },    // STLT_SNOWCROSS
  20.     {    TEXT("Vertical"),        2,        3,        1.0f,    0.96f,    _Rad(00.0f),    false,    },    // STLT_VERTICAL
  21. } ;
  22. static int s_nLibStarDefs = sizeof(s_aLibStarDef) / sizeof(STARDEF) ;
  23.  
  24.  
  25. // Static glare library information
  26. static GLAREDEF s_aLibGlareDef[NUM_GLARELIBTYPES] =
  27. {
  28.     //    glare name                                glare    bloom    ghost    distort    star    star type
  29.     //    rotate            C.A        current    after    ai lum
  30.     {    TEXT("Disable"),                        0.0f,    0.0f,    0.0f,    0.01f,    0.0f,    STLT_DISABLE,
  31.         _Rad(0.0f),        0.5f,    0.00f,    0.00f,    0.0f,  },    // GLT_DISABLE
  32.  
  33.     {    TEXT("Camera"),                            1.5f,    1.2f,    1.0f,    0.00f,    1.0f,    STLT_CROSS,
  34.         _Rad(00.0f),    0.5f,    0.25f,    0.90f,    1.0f,  },    // GLT_CAMERA
  35.     {    TEXT("Natural Bloom"),                    1.5f,    1.2f,    0.0f,    0.00f,    0.0f,    STLT_DISABLE,
  36.         _Rad(00.0f),    0.0f,    0.40f,  0.85f,    0.5f,  },    // GLT_NATURAL
  37.     {    TEXT("Cheap Lens Camera"),                1.25f,    2.0f,    1.5f,    0.05f,    2.0f,    STLT_CROSS,
  38.         _Rad(00.0f),    0.5f,    0.18f,    0.95f,    1.0f,  },    // GLT_CHEAPLENS
  39. /*    
  40.     {    TEXT("Afterimage"),                        1.5f,    1.2f,    0.5f,    0.00f,    0.7f,    STLT_CROSS,
  41.         _Rad(00.0f),    0.5f,    0.1f,    0.98f,    2.0f,  },    // GLT_AFTERIMAGE
  42. */
  43.     {    TEXT("Cross Screen Filter"),            1.0f,    2.0f,    1.7f,    0.00f,    1.5f,    STLT_CROSSFILTER,
  44.         _Rad(25.0f),    0.5f,    0.20f,    0.93f,    1.0f,  },    // GLT_FILTER_CROSSSCREEN
  45.     {    TEXT("Spectral Cross Filter"),            1.0f,    2.0f,    1.7f,    0.00f,    1.8f,    STLT_CROSSFILTER,
  46.         _Rad(70.0f),    1.5f,    0.20f,    0.93f,    1.0f,  },    // GLT_FILTER_CROSSSCREEN_SPECTRAL
  47.     {    TEXT("Snow Cross Filter"),                1.0f,    2.0f,    1.7f,    0.00f,    1.5f,    STLT_SNOWCROSS,
  48.         _Rad(10.0f),    0.5f,    0.20f,    0.93f,    1.0f,  },    // GLT_FILTER_SNOWCROSS
  49.     {    TEXT("Spectral Snow Cross"),            1.0f,    2.0f,    1.7f,    0.00f,    1.8f,    STLT_SNOWCROSS,
  50.         _Rad(40.0f),    1.5f,    0.20f,    0.93f,    1.0f,  },    // GLT_FILTER_SNOWCROSS_SPECTRAL
  51.     {    TEXT("Sunny Cross Filter"),                1.0f,    2.0f,    1.7f,    0.00f,    1.5f,    STLT_SUNNYCROSS,
  52.         _Rad(00.0f),    0.5f,    0.20f,    0.93f,    1.0f,  },    // GLT_FILTER_SUNNYCROSS
  53.     {    TEXT("Spectral Sunny Cross"),            1.0f,    2.0f,    1.7f,    0.00f,    1.8f,    STLT_SUNNYCROSS,
  54.         _Rad(45.0f),    1.5f,    0.20f,    0.93f,    1.0f,  },    // GLT_FILTER_SUNNYCROSS_SPECTRAL
  55.     {    TEXT("Cine Camera Vertical Slits"),        1.0f,    2.0f,    1.5f,    0.00f,    1.0f,    STLT_VERTICAL,
  56.         _Rad(90.0f),    0.5f,    0.20f,    0.93f,    1.0f,  },    // GLT_CINECAM_VERTICALSLIT
  57.     {    TEXT("Cine Camera Horizontal Slits"),    1.0f,    2.0f,    1.5f,    0.00f,    1.0f,    STLT_VERTICAL,
  58.         _Rad(00.0f),    0.5f,    0.20f,    0.93f,    1.0f,  },    // GLT_CINECAM_HORIZONTALSLIT
  59. } ;
  60. static int s_nLibGlareDefs = sizeof(s_aLibGlareDef) / sizeof(GLAREDEF) ;
  61.  
  62.  
  63.  
  64. //----------------------------------------------------------
  65. // Information object for star generation 
  66.  
  67. CStarDef    *CStarDef::ms_pStarLib    = NULL ;
  68. D3DXCOLOR    CStarDef::ms_avChromaticAberrationColor[] ;
  69.  
  70.  
  71. CStarDef::CStarDef()
  72. {
  73.     Construct() ;
  74. }
  75.  
  76. CStarDef::CStarDef(const CStarDef& src)
  77. {
  78.     Construct() ;
  79.     Initialize(src) ;
  80. }
  81.  
  82. CStarDef::~CStarDef()
  83. {
  84.     Destruct() ;
  85. }
  86.  
  87.  
  88. HRESULT CStarDef::Construct()
  89. {
  90.     ZeroMemory( m_strStarName, sizeof(m_strStarName) );
  91.  
  92.     m_nStarLines    = 0 ;
  93.     m_pStarLine        = NULL ;
  94.     m_fInclination    = 0.0f ;
  95.  
  96.     m_bRotation        = false ;
  97.  
  98.     return S_OK ;
  99. }
  100.  
  101. void CStarDef::Destruct()
  102. {
  103.     Release() ;
  104. }
  105.  
  106. void CStarDef::Release()
  107. {
  108.     SAFE_DELETE_ARRAY(m_pStarLine) ;
  109.     m_nStarLines = 0 ;
  110. }
  111.  
  112.  
  113. HRESULT CStarDef::Initialize(const CStarDef& src)
  114. {
  115.     if (&src == this) {
  116.         return S_OK ;
  117.     }
  118.  
  119.     // Release the data
  120.     Release() ;
  121.  
  122.     // Copy the data from source
  123.     lstrcpyn( m_strStarName, src.m_strStarName, 255 );
  124.     m_nStarLines    = src.m_nStarLines ;
  125.     m_fInclination    = src.m_fInclination ;
  126.     m_bRotation        = src.m_bRotation ;
  127.  
  128.     m_pStarLine = new STARLINE[m_nStarLines] ;
  129.     for (int i = 0 ; i < m_nStarLines ; i ++) {
  130.         m_pStarLine[i] = src.m_pStarLine[i] ;
  131.     }
  132.  
  133.     return S_OK ;
  134. }
  135.  
  136.  
  137. /// generic simple star generation
  138. HRESULT CStarDef::Initialize(const TCHAR *szStarName,
  139.                              int nStarLines,
  140.                              int nPasses,
  141.                              float fSampleLength,
  142.                              float fAttenuation,
  143.                              float fInclination,
  144.                              bool bRotation)
  145. {
  146.     // Release the data
  147.     Release() ;
  148.  
  149.     // Copy from parameters
  150.     lstrcpyn( m_strStarName, szStarName, 255 );
  151.     m_nStarLines    = nStarLines ;
  152.     m_fInclination    = fInclination ;
  153.     m_bRotation        = bRotation ;
  154.  
  155.     m_pStarLine = new STARLINE[m_nStarLines] ;
  156.  
  157.     float fInc = D3DXToRadian(360.0f / (float)m_nStarLines) ;
  158.     for (int i = 0 ; i < m_nStarLines ; i ++)
  159.     {
  160.         m_pStarLine[i].nPasses            = nPasses ;
  161.         m_pStarLine[i].fSampleLength    = fSampleLength ;
  162.         m_pStarLine[i].fAttenuation        = fAttenuation ;
  163.         m_pStarLine[i].fInclination        = fInc * (float)i ;
  164.     }
  165.  
  166.     return S_OK ;
  167. }
  168.  
  169.  
  170. /// Specific start generation
  171. //  Sunny cross filter
  172. HRESULT CStarDef::Initialize_SunnyCrossFilter(const TCHAR *szStarName,
  173.                                               float fSampleLength,
  174.                                               float fAttenuation,
  175.                                               float fLongAttenuation,
  176.                                               float fInclination)
  177. {
  178.     // Release the data
  179.     Release() ;
  180.  
  181.     // Create parameters
  182.     lstrcpyn( m_strStarName, szStarName, 255 );
  183.     m_nStarLines    = 8 ;
  184.     m_fInclination    = fInclination ;
  185. //    m_bRotation        = true ;
  186.     m_bRotation        = false ;
  187.  
  188.     m_pStarLine = new STARLINE[m_nStarLines] ;
  189.     float fInc = D3DXToRadian(360.0f / (float)m_nStarLines) ;
  190.     for (int i = 0 ; i < m_nStarLines ; i ++)
  191.     {
  192.         m_pStarLine[i].fSampleLength    = fSampleLength ;
  193.         m_pStarLine[i].fInclination        = fInc * (float)i + D3DXToRadian(0.0f) ;
  194.  
  195.         if ( 0 == (i % 2) ) {
  196.             m_pStarLine[i].nPasses        = 3 ;
  197.             m_pStarLine[i].fAttenuation    = fLongAttenuation ;    // long
  198.         }
  199.         else {
  200.             m_pStarLine[i].nPasses        = 3 ;
  201.             m_pStarLine[i].fAttenuation    = fAttenuation ;
  202.         }
  203.     }
  204.  
  205.     return S_OK ;
  206. }
  207.  
  208. HRESULT CStarDef::InitializeStaticStarLibs()
  209. {
  210.     if (ms_pStarLib) {
  211.         return S_OK ;
  212.     }
  213.  
  214.     ms_pStarLib = new CStarDef[NUM_STARLIBTYPES] ;
  215.  
  216.     // Create basic form
  217.     for (int i = 0 ; i < NUM_BASESTARLIBTYPES ; i ++) {
  218.         ms_pStarLib[i].Initialize(s_aLibStarDef[i]) ;
  219.     }
  220.  
  221.     // Create special form
  222.     // Sunny cross filter
  223.     ms_pStarLib[STLT_SUNNYCROSS].Initialize_SunnyCrossFilter() ;
  224.  
  225.     // Initialize color aberration table
  226. /*
  227.     {
  228.         D3DXCOLOR(0.5f, 0.5f, 0.5f,  0.0f),
  229.         D3DXCOLOR(1.0f, 0.2f, 0.2f,  0.0f),
  230.         D3DXCOLOR(0.2f, 0.6f, 0.2f,  0.0f),
  231.         D3DXCOLOR(0.2f, 0.2f, 1.0f,  0.0f),
  232.     } ;
  233. */
  234.     D3DXCOLOR avColor[8] =
  235.     {
  236. /*
  237.         D3DXCOLOR(0.5f, 0.5f, 0.5f,  0.0f),
  238.         D3DXCOLOR(0.3f, 0.3f, 0.8f,  0.0f),
  239.         D3DXCOLOR(0.2f, 0.2f, 1.0f,  0.0f),
  240.         D3DXCOLOR(0.2f, 0.4f, 0.5f,  0.0f),
  241.         D3DXCOLOR(0.2f, 0.6f, 0.2f,  0.0f),
  242.         D3DXCOLOR(0.5f, 0.4f, 0.2f,  0.0f),
  243.         D3DXCOLOR(0.7f, 0.3f, 0.2f,  0.0f),
  244.         D3DXCOLOR(1.0f, 0.2f, 0.2f,  0.0f),
  245. */
  246.  
  247.         D3DXCOLOR(0.5f, 0.5f, 0.5f,  0.0f),    // w
  248.         D3DXCOLOR(0.8f, 0.3f, 0.3f,  0.0f),
  249.         D3DXCOLOR(1.0f, 0.2f, 0.2f,  0.0f),    // r
  250.         D3DXCOLOR(0.5f, 0.2f, 0.6f,  0.0f),
  251.         D3DXCOLOR(0.2f, 0.2f, 1.0f,  0.0f),    // b
  252.         D3DXCOLOR(0.2f, 0.3f, 0.7f,  0.0f),
  253.         D3DXCOLOR(0.2f, 0.6f, 0.2f,  0.0f),    // g
  254.         D3DXCOLOR(0.3f, 0.5f, 0.3f,  0.0f),
  255.     } ;
  256.  
  257.     memcpy( ms_avChromaticAberrationColor, avColor, sizeof(D3DXCOLOR) * 8 ) ;
  258. /*
  259.     ms_avChromaticAberrationColor[0] = D3DXCOLOR(0.5f, 0.5f, 0.5f,  0.0f) ;
  260.     ms_avChromaticAberrationColor[1] = D3DXCOLOR(0.7f, 0.3f, 0.3f,  0.0f) ;
  261.     ms_avChromaticAberrationColor[2] = D3DXCOLOR(1.0f, 0.2f, 0.2f,  0.0f) ;
  262.     ms_avChromaticAberrationColor[3] = D3DXCOLOR(0.5f, 0.5f, 0.5f,  0.0f) ;
  263.     ms_avChromaticAberrationColor[4] = D3DXCOLOR(0.2f, 0.6f, 0.2f,  0.0f) ;
  264.     ms_avChromaticAberrationColor[5] = D3DXCOLOR(0.2f, 0.4f, 0.5f,  0.0f) ;
  265.     ms_avChromaticAberrationColor[6] = D3DXCOLOR(0.2f, 0.3f, 0.8f,  0.0f) ;
  266.     ms_avChromaticAberrationColor[7] = D3DXCOLOR(0.2f, 0.2f, 1.0f,  0.0f) ;
  267. */
  268.     return S_OK ;
  269. }
  270.  
  271. HRESULT CStarDef::DeleteStaticStarLibs()
  272. {
  273.     // Delete all libraries
  274.     SAFE_DELETE_ARRAY( ms_pStarLib ) ;
  275.  
  276.     return S_OK ;
  277. }
  278.  
  279.  
  280.  
  281. //----------------------------------------------------------
  282. // Glare definition
  283.  
  284. CGlareDef    *CGlareDef::ms_pGlareLib    = NULL ;
  285.  
  286. CGlareDef::CGlareDef()
  287. {
  288.     Construct() ;
  289. }
  290.  
  291. CGlareDef::CGlareDef(const CGlareDef& src)
  292. {
  293.     Construct() ;
  294.     Initialize(src) ;
  295. }
  296.  
  297. CGlareDef::~CGlareDef()
  298. {
  299.     Destruct() ;
  300. }
  301.  
  302.  
  303. HRESULT CGlareDef::Construct()
  304. {
  305.     ZeroMemory( m_strGlareName, sizeof(m_strGlareName) );
  306.  
  307.     m_fGlareLuminance    = 0.0f ;
  308.     m_fBloomLuminance    = 0.0f ;
  309.     m_fGhostLuminance    = 0.0f ;
  310.     m_fStarLuminance    = 0.0f ;
  311.     m_fStarInclination    = 0.0f ;
  312.     m_fChromaticAberration    = 0.0f ;
  313.  
  314.     m_fAfterimageSensitivity    = 0.0f ;
  315.     m_fAfterimageRatio            = 0.0f ;
  316.     m_fAfterimageLuminance        = 0.0f ;
  317.  
  318.     return S_OK ;
  319. }
  320.  
  321. void CGlareDef::Destruct()
  322. {
  323.     m_starDef.Release() ;
  324. }
  325.  
  326. void CGlareDef::Release()
  327. {
  328. }
  329.  
  330. HRESULT CGlareDef::Initialize(const CGlareDef& src)
  331. {
  332.     if (&src == this) {
  333.         return S_OK ;
  334.     }
  335.  
  336.     // Release the data
  337.     Release() ;
  338.  
  339.     // Copy data from source
  340.     lstrcpyn( m_strGlareName, src.m_strGlareName, 255 );
  341.     m_fGlareLuminance    = src.m_fGlareLuminance ;
  342.  
  343.     m_fBloomLuminance    = src.m_fBloomLuminance ;
  344.     m_fGhostLuminance    = src.m_fGhostLuminance ;
  345.     m_fGhostDistortion    = src.m_fGhostDistortion ;
  346.     m_fStarLuminance    = src.m_fStarLuminance ;
  347.     m_fStarLuminance    = src.m_fStarLuminance ;
  348.     m_fStarInclination    = src.m_fStarInclination ;
  349.     m_fChromaticAberration    = src.m_fChromaticAberration ;
  350.  
  351.     m_fAfterimageSensitivity    = src.m_fStarLuminance ;
  352.     m_fAfterimageRatio            = src.m_fStarLuminance ;
  353.     m_fAfterimageLuminance        = src.m_fStarLuminance ;
  354.  
  355.     m_starDef            = src.m_starDef ;
  356.  
  357.     return S_OK ;
  358. }
  359.  
  360.  
  361. HRESULT CGlareDef::Initialize(const TCHAR *szStarName,
  362.                               float fGlareLuminance,
  363.                               float fBloomLuminance,
  364.                               float fGhostLuminance,
  365.                               float fGhostDistortion,
  366.                               float fStarLuminance,
  367.                               ESTARLIBTYPE eStarType,
  368.                               float fStarInclination,
  369.                               float fChromaticAberration,
  370.                               float fAfterimageSensitivity,    // Current weight
  371.                               float fAfterimageRatio,        // Afterimage weight
  372.                               float fAfterimageLuminance)
  373. {
  374.     // Release the data
  375.     Release() ;
  376.  
  377.     // Create parameters
  378.     lstrcpyn( m_strGlareName, szStarName, 255 );
  379.     m_fGlareLuminance    = fGlareLuminance ;
  380.  
  381.     m_fBloomLuminance    = fBloomLuminance ;
  382.     m_fGhostLuminance    = fGhostLuminance ;
  383.     m_fGhostDistortion    = fGhostDistortion ;
  384.     m_fStarLuminance    = fStarLuminance ;
  385.     m_fStarInclination    = fStarInclination ;
  386.     m_fChromaticAberration    = fChromaticAberration ;
  387.  
  388.     m_fAfterimageSensitivity    = fAfterimageSensitivity ;
  389.     m_fAfterimageRatio            = fAfterimageRatio ;
  390.     m_fAfterimageLuminance        = fAfterimageLuminance ;
  391.  
  392.     // Create star form data
  393.     m_starDef            = CStarDef::GetLib(eStarType) ;
  394.  
  395.     return S_OK ;
  396. }
  397.  
  398.  
  399. HRESULT CGlareDef::InitializeStaticGlareLibs()
  400. {
  401.     if (ms_pGlareLib) {
  402.         return S_OK ;
  403.     }
  404.  
  405.     CStarDef::InitializeStaticStarLibs() ;
  406.     ms_pGlareLib = new CGlareDef[NUM_GLARELIBTYPES] ;
  407.  
  408.     // Create glare form
  409.     for (int i = 0 ; i < NUM_GLARELIBTYPES ; i ++) {
  410.         ms_pGlareLib[i].Initialize(s_aLibGlareDef[i]) ;
  411.     }
  412.  
  413.     return S_OK ;
  414. }
  415.  
  416.  
  417. HRESULT CGlareDef::DeleteStaticGlareLibs()
  418. {
  419.     // Delete all libraries
  420.     SAFE_DELETE_ARRAY( ms_pGlareLib ) ;
  421.  
  422.     return S_OK ;
  423. }
  424.  
  425.  
  426. //----------------------------------------------------------
  427.  
  428. // Dummy to generate static object for glare
  429. __CGlare_GenerateStaticObjects __CGlare_GenerateStaticObjects::ms_staticObject ;
  430.